package com.app.megaprosoft.modyimcarcare;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.megaprosoft.modyimcarcare.R;



import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DigitalClock;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class Detail extends Activity {
	
	connect con = new connect(); 
	ResultSet rs,rs1,rs2,rs3,rs4,rs10;
	SimpleAdapter adapter;  
	int chackTypr;
	TextView RegID,Bnd,Gen,Clr,SvL,Stm,EdT,Sts,showCompany;
	ListView listVive1,listVive2,listVive3,listVive4;
	Cursor myData;
	String[] Cmd = {"Edit","Delete","Cancal"};
	String id,ID,tus,typeSer,washCount,washCountShow,NameInput,company,CompanyID,DTID;
	Button test;
	
	DigitalClock digitalCk;
	
	
	 List<Map<String,String>>
	 data=new ArrayList<Map<String,String>>();
	 
	 List<Map<String,String>>
	 data2=new ArrayList<Map<String,String>>();
	 
	 List<Map<String,String>>
	 data3=new ArrayList<Map<String,String>>();
	 
	 List<Map<String,String>>
	 data4=new ArrayList<Map<String,String>>();
	 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		setContentView(R.layout.activity_detail);
		
		digitalCk = (DigitalClock) findViewById(R.id.digitalClock1);
		
		NameInput = getIntent().getExtras().getString("name");
		company = getIntent().getExtras().getString("CompanyS");
		
	//	digitalCk.setTypeface(bge_currentDateandTimeFont_4inches);
		showCompany = (TextView) findViewById(R.id.showCompany);
		
		showCompany.setText(company);
		
		ShowListData();
		ShowWash();
		ShowHole();
		ShowCheck();
		
		
		  final ImageView bth = (ImageView) findViewById(R.id.BTH);
			
			bth.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
				 
					//Open Form Add
					Intent home = new Intent(Detail.this,MainActivity.class);
					home.putExtra("name", NameInput);
					home.putExtra("CompanyS", company);
					startActivity(home);
					
					finish();

				}
			});
			
			
			
		}	
		
		
		
	
	
	
	public void ShowListData(){
	listVive1 = (ListView) findViewById(R.id.listViewDetail);
	
    try{
    	
    	String sqlCompany ="SELECT * FROM Company WHERE CompanyName ='"+company+"' ";  
		
		rs10 = con.selectData(sqlCompany);
		try {
			rs10.next();
			
			CompanyID = rs10.getString("OID");
			
		} catch (SQLException e1) {
			
			e1.printStackTrace();
		}
		
   
    rs = con.selectData("SELECT * FROM ServiceDetail_old WHERE CompanyID ='"+CompanyID+"' ");
    
	      while (rs.next()) {
	
// listView1
	    	Map<String,String>datanum=new HashMap<String,String>();
            datanum.put("DTID", rs.getString("DTID"));
 	        datanum.put("ServiceID", rs.getString("ServiceID")); 
	        datanum.put("ServiceType", rs.getString("ServiceType"));
	        datanum.put("RegisterID", rs.getString("RegisterID"));
	        datanum.put("Brand", rs.getString("Brand"));
	        datanum.put("Generation", rs.getString("Generation"));
	        datanum.put("Color", rs.getString("Color"));
	        datanum.put("StartTime", rs.getString("StartTime"));
	        datanum.put("EndTime", rs.getString("EndTime"));
	        datanum.put("Status", rs.getString("Status"));
	        datanum.put("WetTime", rs.getString("WetTime"));
	        datanum.put("DryTime", rs.getString("DryTime"));
	        data.add(datanum);
	 
	  
	  
	  
	  
	  
        
    }

    adapter = new SimpleAdapter(Detail.this,data, R.layout.activity_coldetail,
             new String[] { "DTID","RegisterID", "ServiceType"
     					, "StartTime", "EndTime", "Status","WetTime","DryTime"}, new int[] {R.id.CNum,R.id.CRegID
     					, R.id.CLsSerc, R.id.CStrTm, R.id.CEndTm, R.id.CSats,R.id.WetTime,R.id.DryTime});
    

     listVive1.setAdapter(adapter);
     final TextView all = (TextView) findViewById(R.id.all);
          
     int countItemListView = listVive1.getCount();
    
     String countItem = Integer.toString ( countItemListView );
    
     all.setText(countItem);

     final AlertDialog.Builder viewDetail = new AlertDialog.Builder(this);

	 listVive1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

	        public boolean onItemLongClick(AdapterView<?> arg0, View v,
	                int index, long arg3) {
	            // TODO Auto-generated method stub
	        	
	        	 id = data.get(index).get("RegisterID");
	             ID = data.get(index).get("ServiceID");
	        	 tus = data.get(index).get("Status");
	        	 typeSer = data.get(index).get("ServiceType");
	        	 DTID = data.get(index).get("DTID");
	        	 
		    	  viewDetail.setIcon(android.R.drawable.btn_star_big_on);
		    	  viewDetail.setTitle("����¹�ŧʶҹ�");
		    	  viewDetail.setMessage("ö�ѹ��� ����¹ : "+ id);
		    	
		    	   
		    	  viewDetail.setPositiveButton("��ŧ",new DialogInterface.OnClickListener() {
		    		  
		    		  			
								public void onClick(DialogInterface dialog, int which) {
									// TODO Auto-generated method stub
									
								
									
								//if(ID.equals(id))	
								{
								Intent goCheck = new Intent(Detail.this,CheckActivity.class);
								goCheck.putExtra("TID", DTID);
							    goCheck.putExtra("DTID", ID);
								goCheck.putExtra("RegisterID", id);
								goCheck.putExtra("Status", tus);
								goCheck.putExtra("Type", typeSer);
								goCheck.putExtra("name", NameInput);
								goCheck.putExtra("CompanyS", company);
								startActivity(goCheck);
								
								Toast.makeText(Detail.this,"���������¹�ŧʶҹ�",
			               	 		     Toast.LENGTH_LONG).show(); 
									
									
									
								}
									
									
								dialog.dismiss();
								 
								
								}
							});
		    	  
		    	  
			    	  viewDetail.setNeutralButton("¡��ԡ", new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							
					
							
						}
					});
			    	  
			    	  viewDetail.show();		    	  
			  		
	            return true;
	        }
	});
   
    }catch(Exception e){ 
 	   
       Log.e("ERROR", e.getMessage()); 
   }
} 
	
	
	public void ShowWash(){
		listVive2 = (ListView) findViewById(R.id.listViewDetailWash);
		
	    try{	   
	    rs2 = con.selectData("SELECT ServiceType FROM ServiceDetail_old WHERE ServiceType='��ԡ����ҧ' " +
	    		"and CompanyID ='"+CompanyID+"' ");
	    		    
	    while (rs2.next()) {
		
		    	Map<String,String>datanum1=new HashMap<String,String>();
		        datanum1.put("ServiceType", rs2.getString("ServiceType"));
		        
		        data2.add(datanum1);  
	        
	    }

	    adapter = new SimpleAdapter(Detail.this,data2, R.layout.activity_coldetail,
	             new String[] { "ServiceType"}, new int[] {R.id.CLsSerc});
	    

		      listVive2.setAdapter(adapter);
	    
	     final TextView wash = (TextView) findViewById(R.id.wash);

	     int countItemListView = listVive2.getCount();
 
	     String countItem = Integer.toString ( countItemListView );
	   
	     
	     
	     wash.setText(countItem);

	    }catch(Exception e){ 
	 	   
	       Log.e("ERROR", e.getMessage()); 
	   }
	}	
	
	public void ShowHole(){
		listVive3 = (ListView) findViewById(R.id.listViewDetailHole);
		
	    try{	   
	    rs3 = con.selectData("SELECT ServiceType FROM ServiceDetail_old WHERE ServiceType='��ԡ������'" +
	    		" and CompanyID ='"+CompanyID+"' ");
	    		    
	    while (rs3.next()) {
		
		    	Map<String,String>datanum2=new HashMap<String,String>();
		        datanum2.put("ServiceType", rs3.getString("ServiceType"));
		        
		        data3.add(datanum2);  
	        
	    }

	    adapter = new SimpleAdapter(Detail.this,data3, R.layout.activity_coldetail,
	             new String[] { "ServiceType"}, new int[] {R.id.CLsSerc});
	    

		      listVive3.setAdapter(adapter);
	    
	     final TextView hole = (TextView) findViewById(R.id.hole);

	     int countItemListView = listVive3.getCount();
 
	     String countItem = Integer.toString ( countItemListView );
	   
	     
	     
	     hole.setText(countItem);

	    }catch(Exception e){ 
	 	   
	       Log.e("ERROR", e.getMessage()); 
	   }
	}	
	
	public void ShowCheck(){
		listVive4 = (ListView) findViewById(R.id.listViewDetailCheck);
		
	    try{	   
	    rs4 = con.selectData("SELECT Status FROM ServiceDetail_old WHERE Status='��Ǩ�����' " +
	    		"and CompanyID ='"+CompanyID+"' ");
	    		    
	    while (rs4.next()) {
		
		    	Map<String,String>datanum1=new HashMap<String,String>();
		        datanum1.put("Status", rs4.getString("Status"));
		        
		        data4.add(datanum1);  
	        
	    }

	    adapter = new SimpleAdapter(Detail.this,data4, R.layout.activity_coldetail,
	             new String[] { "Status"}, new int[] {R.id.CLsSerc});
	    

		      listVive4.setAdapter(adapter);
	    
	     final TextView check = (TextView) findViewById(R.id.check);

	     int countItemListView = listVive4.getCount();
 
	     String countItem = Integer.toString ( countItemListView );
	   
	     
	     
	     check.setText(countItem);

	    }catch(Exception e){ 
	 	   
	       Log.e("ERROR", e.getMessage()); 
	   }
	}	
	@Override
	public void onBackPressed() {
	  
	        new AlertDialog.Builder(this)
	        .setTitle("¡��ԡ��÷���¡��")
	        .setMessage("��Ѻ�˹����ѡ")
	                .setNegativeButton(android.R.string.yes, new DialogInterface.OnClickListener() {

	                    public void onClick(DialogInterface arg0, int arg1) {
	                        Intent home = new Intent(getApplicationContext(),MainActivity.class);	                          
	                    	home.putExtra("name", NameInput);
	                		home.putExtra("CompanyS", company);
	                        startActivity(home);
	                        finish();
	                        overridePendingTransition(R.layout.fadeendout,R.layout.fadeendin);
	                    }
	                })
	                .setPositiveButton(android.R.string.no, null).create().show();
	    

	}
	
	
	
}